const db = require('../tool/db.js');

async function add(data) {
    if (!data.name || data.name.length < 5 || data.name.length > 30) {
        return { code: 0, msg: '商品名称应该在5-30个字符之间', data: '' }
    }
    if (!data.describes || data.describes.length < 10 || data.describes.length > 50) {
        return { code: 0, msg: '商品描述应该在10-50个字符之间', data: '' }
    }
    if (!data.price) {
        return { code: 0, msg: '商品价格不能为空', data: '' }
    }
    if (!data.stock) {
        return { code: 0, msg: '商品库存不能为空', data: '' }
    }
    if (!data.cid) {
        return { code: 0, msg: '商品分类不能为空', data: '' }
    }
    if (!data.photo) {
        return { code: 0, msg: '商品图片不能为空', data: '' }
    }
    // let sql = `insert into goods (number,name,price,cid,stock,photo,describes) values(${data.number},${data.name},${data.price},${data.cid},${data.stock},${data.photo},${ data.describes})`
    let sql = 'insert into goods (number,name,price,cid,stock,photo,describes) values(?,?,?,?,?,?,?)'
    // console.log(sql);   
     let [result] = await db.query(sql, [data.number, data.name, data.price, data.cid, data.stock,("'"+ data.photo +"'"), data.describes]);
    if (result.insertId) {
        return { code: 1, msg: '添加成功', data: result.insertId }
    } else {
        return { code: 0, msg: '添加失败', data: '' }
    }
}


/**
 * 获取商品列表
 * @param {*} page 当前页码
 * @param {*} num 每页数量
 * @param {*} options 筛选条件
 */
async function list(page, num,options) {
    //筛选条件的处理
    let where = ' WHERE 1 ';
    if (options.cid) {
        where += `and cid=${options.cid}`
    }
    if (options.name) {
        where += `and goods.name like "%${options.name}%" `
    }
    // 分页偏移量
    let offset = (page - 1) * num;
    let sql = `select goods.* , category.name as cname  from goods join category on goods.cid
     = category.id  ${where} LIMIT ${offset},${num}`;

    let [lists] = await db.query(sql);

    // 查询符合条件的总数量
    let [[{ total }]] = await db.query(`SELECT COUNT(*) as total FROM goods ${where}`)
    return { lists, total }
}

async function mod(data){
    // console.log(data);
    if (!data.name || data.name.length < 5 || data.name.length > 30) {
        return { code: 0, msg: '商品名称应该在5-30个字符之间', data: '' }
    }
    if (!data.describes || data.describes.length < 10 || data.describes.length > 50) {
        return { code: 0, msg: '商品描述应该在10-50个字符之间', data: '' }
    }
    if (!data.price) {
        return { code: 0, msg: '商品价格不能为空', data: '' }
    }
    if (!data.stock) {
        return { code: 0, msg: '商品库存不能为空', data: '' }
    }
    if (!data.cid) {
        return { code: 0, msg: '商品分类不能为空', data: '' }
    }
    if (!data.photo) {
        return { code: 0, msg: '商品图片不能为空', data: '' }
    }
    data.photo = `'${data.photo}'`
    // console.log(typeof data.photo);
    // let sql=`update goods set name='${data.name}',price=${data.price},describes='${data.describes}',cid=${data.cid},stock=${data.stock},photo=${data.photo} where id=${data.id}`
    let sql=`update goods set name= ?,price=?,describes=?,cid=?,stock=?,photo=? where id=?`
    let [result]=await db.query(sql,[data.name,data.price,data.describes,data.cid,data.stock,data.photo,data.id])

    if (result.affectedRows){
        return {code:1, msg:'修改成功', data:''}
    }else{
        return {code:0, msg:'修改失败', data:''}
    }
}

async function get(data){
    let sql=`select * from goods where id=${data.id}` 
    let [[res]] = await db.query(sql);
    return res;
}

// async function del(data){
//     let sql=`delete from goods where id=${data.id}`
//     let res = await db.query(sql);
//     return res;
// }
module.exports = { add, list,mod,get }